<template>
	<view class="uni_box home">
		<navbar title='资产' backgroundColor="#f8f8f8" :isback="false" :yin="false">
		</navbar>
		<view class="content">
			<view class="mone">
				<image class="mbg" src="/static/img/mbg.png" />
				<view class="moview">
					<view class="zileft">
						<view class="zione">
							<text class="zname">总资产估值（USDT）</text>
							<image class="yan" src="/static/img/yan.png" v-if="!iseyes" @click="geteyes()" />
							<image class="yan" src="/static/img/yans.png" v-if="iseyes"  @click="geteyes()" />
						</view>
						<text class="zinum">{{iseyes?'****':formatNumbertwo(uinfo.data[0].money)}}</text>
						<text class="zinumtwo" v-if="Equivalent">{{ iseyes?'****':'≈'+formatNumbertwo(Equivalent)+'  USD' }}</text>
					</view>
					<view class="asset">
						<view class="assetitem" @click="navClick(1)">
							<image class="asseticon" src="/static/img/mone.png" />
							<text>充币</text>
						</view>
						<view class="assetitem" @click="navClick(2)">
							<image class="asseticon" src="/static/img/mtwo.png" />
							<text>提币</text>
						</view>
						<!-- <view class="assetitem" @click="navClick(3)">
							<image class="asseticon" src="/static/img/mthree.png" />
							<text>账单</text>
						</view> -->
					</view>
				</view>
			</view>

			<view class="mtwo">
				<view class="assetlist" v-for="(item,index) in balance" :key="index" @click="getDetail(item)">
					<image class="bicon" :src="item.currency.logo" />
					<view class="mlright">
						<view class="rone">
							<text class="rname">{{item.currency.name}}</text>
							<image class="mfan" src="/static/img/mfan.png" />
						</view>
						<view class="rone">
							<text class="rnametwo">可用</text>
							<text class="rval">{{ iseyes?'****':item.money==0?'0.00':formatNumbertwo(item.money) }}</text>
						</view>
						<view class="rone">
							<text class="rnametwo">折合(USDT)</text>
							<text class="rval">{{ iseyes?'****':getjisuan(item.money)==0?'0.00':formatNumbertwo(getjisuan(item.money)) }}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				info:{
					// balance:[],
					profile:{}
				},
				sinfo:{},
				uinfo:{
					total:0,
					data:[
						{money:0.00}
					]
				},
				iseyes:false,
				Equivalent:'',
				balance:[]
			}
		},
		onLoad() {
			if(uni.getStorageSync('token')){
				this.getuserinfo();
				this.getstatistic()
				this.getstatistictwo()
			}
			
			
			// console.log(JSON.parse(uni.getStorageSync('balance')))
		},
		onShow() {
			if(uni.getStorageSync('token')){
				if(uni.getStorageSync('balance')){
					this.balance = JSON.parse(uni.getStorageSync('balance'))
					// uni.setStorageSync('balance',JSON.stringify(this.mineinfo.balance))
				}
				this.getstatistictwo()
			}else{
				this.navrouter('/pages/login/login?type=mine')
			}
			
		},
		onTabItemTap(item) {
			console.log(item)
			// 通过item.index或者item.pagePath判断是哪个tabbar item被点击
			if (item.index == 2) { // 假设我们关注第二个tabbar item
				if(!uni.getStorageSync('token')){
					uni.navigateTo({
						url: '/pages/login/login?type=mine'
					})
				}
			}
		},
		methods: {
			async getuserinfo(){
				let res = await this.$u.api.userinfo({
				});
				this.info = res
				if(JSON.stringify(this.info.balance)!=uni.getStorageSync('balance')){
					this.balance = this.info.balance
				}
				// this.balance = this.info.balance
				uni.setStorageSync('balance',JSON.stringify(this.info.balance))
			},
			async getstatistic(){
				let res = await this.$u.api.apiconfig({
					// type:'day'
				});
				// console.log(res)
				this.sinfo = res
			},
			async getstatistictwo(){
				let res = await this.$u.api.assets_statistic({
					type:'day'
				});
				// console.log(res)
				this.uinfo = res
				// console.log(uni.getStorageSync('locale'))
				if(uni.getStorageSync('locale')){
					let locale = uni.getStorageSync('locale')
					this.Equivalent = Number(this.uinfo.data[0].money)/Number(locale.rate)
					// console.log(this.Equivalent)
				}else{
					this.getSaveLanguage();
				}
				// uni.setStorageSync('locale',item)
			},
			async getSaveLanguage(){//如果默认没有语言缓存 那就请求 取第一个处理下字段数据
				let resd = await this.$u.api.apiconfig({
				});
				// this.apiinfo = resd
				this.languagelist = resd.language
				let item = this.languagelist[0]
				this.$i18n.locale = item
				// console.log(this.$i18n.locale)
				uni.setStorageSync('locale',item)
				let locale = uni.getStorageSync('locale')
				this.Equivalent = Number(this.uinfo.data[0].money)/Number(locale.rate)
				// console.log(this.Equivalent)
			},
			getDetail(item){
				this.navrouter("/pages/TPage/Funddetails?id="+item.id+'&items='+JSON.stringify(item));
			},
			navClick(e){
				if(e == 1){
					this.navrouter("/pages/TPage/Rechargecoins");
				}else if(e == 2){
					this.navrouter("/pages/TPage/Withdrawalcoins");
				}else if(e == 3){
					this.navrouter("/pages/TPage/billPage");
				}
			},
			geteyes(){
				this.iseyes = !this.iseyes
			},
			containsDecimal(str) {
				return /\d+\.\d+/.test(str);
			},
			formatNumber(num) {
				// console.log(typeof num)
				// 将数字转换为字符串
				
				var numStr = ''
				if(typeof num == 'number'){
					numStr = num.toString();
				}else{
					numStr = num;
				}
				if(!this.containsDecimal(numStr)){
					return Number(num)
				}
				
				// 使用正则表达式检查小数点后是否全为0
				const isDecimalAllZeros = /^\d+(\.\d{6})?0{1,}$/.test(numStr);
				// 根据条件格式化数字
				return isDecimalAllZeros ? Number(num).toFixed(2) : Number(num).toFixed(6);
			},
			getjisuan(price){
				let locale = uni.getStorageSync('locale')
				if(locale == '' || !locale){
					return ''
				}else{
					return Number(price)/Number(locale.rate)
				}
				
			},
			formatNumbertwo(num){
				if(num == 0){
					return '0.00'
				}else{
					return this.toCompressedNumberString(Number(num).toFixed(6))
				}
				
			},
			toCompressedNumberString(num, precision) {
				// 使用toFixed转换为有效的小数点后的位数，并处理特殊的Infinity和NaN值
				let str = Number.isFinite(num) ? num.toFixed(precision) : String(num);
				// 使用正则表达式移除尾部多余的零
				str = str.replace(/\.?0+$/, '');
				// 移除前导零，除非是整数
				str = str.replace(/^0+(?!\.|$)/, '');
				return this.padDecimal(str);
			},
			padDecimal(num) {
				// 将数字转换为字符串
				const numStr = num.toString();
				// 使用正则表达式检查是否有小数点和两位小数
				if (numStr.match(/^\d+(\.\d{1,2})?$/)) {
					// 如果小数点后只有一位数字，则补0
					return numStr.replace(/\.\d$/, '.00').padEnd(4, '0');
				}
				// 如果不是小数或小数点后已经有两位数字，则直接返回原数字
				return num;
			}
		}
	}
</script>

<style lang="scss" scoped>
	page{
		background-color: #fff;
	}
	.content{
		background-color: #fff;
		padding-bottom: 130rpx;
		.mone{
			position: relative;
			height: 430rpx;
			width: 100%;
			.mbg{
				position: absolute;
				width: 100%;
				height: 100%;

			}
			.moview{
				position: relative;
				padding: 60rpx 50rpx 24rpx 60rpx;
				display: flex;
				flex-direction: column;
				height: 100%;
				.zileft{
					display: flex;
					flex-direction: column;
					.zione{
						display: flex;
						flex-direction: row;
						align-items: center;
						.zname{
							font-family: Source Han Sans CN;
							font-weight: bold;
							font-size: 30rpx;
							color: #111111;
							display: inline-block;
							margin-right: 15rpx;
						}
						.yan{
							width: 44rpx;
							height: 30rpx;
						}
					}
					.zinum{
						// margin-top: 43rpx;
						margin: 35rpx 0 25rpx;
						font-family: Source Han Sans CN;
						font-weight: 500;
						font-size: 40rpx;
						color: #000000;
					}
					.zinumtwo{
						font-family: Source Han Sans CN;
						font-weight: 400;
						font-size: 28rpx;
						color: #999999;
					}
				}
				.asset{
					display: flex;
					justify-content: flex-end;
					padding: 0 0rpx;
					flex-direction: row;
					margin-top: auto;
					.assetitem{
						display: flex;
						flex-direction: column;
						align-items: center;
						justify-content: center;
						margin-left: 57rpx;
						.asseticon{
							width: 42rpx;
							height: 42rpx;
							margin-bottom: 8rpx;
						}
						text{
							font-family: Source Han Sans CN;
							font-weight: 400;
							font-size: 24rpx;
							color: #000000;
						}
					}
				}
			}
		}
		.mtwo{
			padding: 60rpx 40rpx;
			.assetlist{
				display: flex;
				flex-direction: row;
				.bicon{
					margin-right: 11rpx;
					width: 44rpx;
					height: 44rpx;
					
				}
				.mlright{
					display: flex;
					flex: 1;
					flex-direction: column;
					.rone{
						display: flex;
						flex-direction: row;
						align-items: center;
						margin-bottom: 50rpx;
						.rname{
							font-family: Source Han Sans CN;
							font-weight: bold;
							font-size: 30rpx;
							color: #000000;
						}
						.mfan{
							margin-left: auto;
							width: 16rpx;
							height: 28rpx;
						}
						.rnametwo{
							font-family: Source Han Sans CN;
							font-weight: 400;
							font-size: 24rpx;
							color: #999999;
						}
						.rval{
							font-family: Source Han Sans CN;
							font-weight: 400;
							font-size: 28rpx;
							color: #000000;
							margin-left: auto;
						}
					}
				}
			}
		}
	}
</style>
